﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using TYAJZWeb.Data;
using TYAJZWeb.Utils;

namespace TYAJZWeb.Models
{
    public class ProjectBaseModel
    {
        public int Id { get; set; }

        [Display(Name = "工程名称")]
        [Required]
        public String Name { get; set; }

        [Display(Name = "工程地址")]
        [Required]
        public String Address { get; set; }

        [Display(Name = "施工单位")]
        [Required]
        public String ConstructionCompany { get; set; }

        [Display(Name = "项目经理")]
        [Required]
        public String ConstructionProjectManager { get; set; }

        [Display(Name = "联系电话")]
        [Required]
        public String ConstructionProjectManagerPhoneMobile { get; set; }


        [Display(Name = "安拆单位")]
        public int DemolitionCompanyId { get; set; }

        [Display(Name = "检测单位")]
        public String InspectCompany { get; set; }

        [Display(Name = "检测时间")]
        public String InspectTime { get; set; }

        [Display(Name = "安装日期")]
        [Required]
        public String ConstructDateTime { get; set; }

        [Display(Name = "计划拆卸日期")]
        public String DemolitionDateTime { get; set; }

        [Display(Name = "建筑高度")]
        [Required]
        public String High { get; set; }

        [Display(Name = "备注")]
        public String Comment { get; set; }

        [Display(Name = "工程状态")]
        public ProjectStatus Status { get; set; }
    }

    public enum InspectResults
    {
        None,
        Failure,
        Pass
    }

    public enum ProjectStatus
    {
        New,
        Working,
        InspectResultSuccess,
        InspectResultFailure,
        ApplyFormFinish,
        ApplyFormPrinted,
        Finish,
        Destroyed
    }

    public class ProjectAddModel : ProjectBaseModel
    {
        public Equipment Equipment;
        //public int EquipmentId
        //{
        //    get
        //    {
        //        return Equipment.Id;
        //    }
        //}

        public ProjectAddModel() { Equipment = new Equipment(); }

        public ProjectAddModel(Equipment e)
        {
            Equipment = e;
            Id = e.Id;
            InspectNO = e.Eq_InspectNO;

            //if (e.IsLocal)
            //    Owner = e.Company.Legal;
            //else
            Owner = e.Eq_InCharge;

            //if (Equipment.IsLocal)
            //    Mobile = Equipment.Company.PhoneOfLegal;
            //else
            Mobile = Equipment.Eq_Phone;

        }

        public String EquipmentName
        {
            get
            {
                return Equipment.EquipmentType.Name;
            }
        }
        public String EquipmentModel
        {
            get
            {
                return Equipment.Eq_Type;
            }
        }
        public String EquipmentNO
        {
            get
            {
                return Equipment.Eq_no;
            }
            set
            {
                Equipment.Eq_no = value;
            }
        }

        public String OwnerCompany
        {
            get
            {
                if (Equipment.IsLocal)
                    return Equipment.Company.Name;
                else
                    return Equipment.Eq_Owner;
            }
        }
        [Display(Name = "产权单位联系人")]
        public String Owner
        {
            get;
            set;
        }
        [Display(Name = "产权单位联系电话")]
        public String Mobile
        {
            get;
            set;
        }

        [Display(Name = "报检号")]
        public String InspectNO
        {
            get;
            set;
        }

        [Display(Name = "安拆单位")]
        public String DemolitionCompany { get; set; }

        [Display(Name = "负责人")]
        public String DemolitionCompanyOwner { get; set; }

        [Display(Name = "电话")]
        public String DemolitionCompanyOwnerPhone { get; set; }

        public static explicit operator Project(ProjectAddModel model)
        {
            return new Project()
            {
                Name = model.Name,
                Address = model.Address,
                ConstructDateTime = Convert.ToDateTime(model.ConstructDateTime),
                InspectTime = new DateTime(1900, 1, 1),
                ConstructionCompany = model.ConstructionCompany ?? "",
                ConstructionProjectManager = model.ConstructionProjectManager ?? "",
                ConstructionProjectManagerPhoneMobile = model.ConstructionProjectManagerPhoneMobile ?? "",
                DemolitionCompanyId = model.DemolitionCompanyId,
                DemolitionCompanyOwner = model.DemolitionCompanyOwner ?? string.Empty,
                DemolitionCompanyOwnerPhone = model.DemolitionCompanyOwnerPhone ?? string.Empty,
                DemolitionDateTime = Convert.ToDateTime(model.DemolitionDateTime ?? "1900-1-1"),
                High = model.High ?? "",
                Status = ProjectStatus.New.ToString(),
                Comment = model.Comment ?? "",
                EquipmentId = model.Equipment.Id,
                InspectDepartment = string.Empty,
                InspectResult = InspectResults.None.ToString(),
                EquipmentMobile = model.Mobile,
                EquipmentOwner = model.Owner
            };
        }
    }

    public class ProjectListModel : ProjectBaseModel
    {

        public String EquipmentNo { get; set; }
        public String EquipmentCertNo { get; set; }
        public InspectResults InspectResult { get; set; }
        public String InspectResultString
        {
            get
            {
                switch (InspectResult)
                {
                    case InspectResults.None:
                        return string.Empty;
                    case InspectResults.Failure:
                        return "未通过";
                    case InspectResults.Pass:
                        return "通过";
                    default:
                        return string.Empty;
                }
            }
        }
        public String InspectNO { get; set; }
        public bool HasPrinted { get; set; }
        public bool NearlyExpired
        {
            get
            {
                if (this.InspectResult == InspectResults.Pass)
                {
                    //return Convert.ToDateTime(InspectTime).AddDays(365) < DateTime.Now.AddDays(30);
                    int diffDays = (DateTime.Now - Convert.ToDateTime(InspectTime)).Days;
                    if (diffDays > 335 && diffDays <= 365)
                        return true;
                }
                return false;
            }
        }

        public bool HasBeenExpired
        {
            get
            {
                if (this.InspectResult == InspectResults.Pass)
                {
                    if ((DateTime.Now - Convert.ToDateTime(InspectTime)).Days > 365)
                        return true;
                }
                return false;
            }
        }

        public string HasPrintedString
        {
            get
            {
                return (HasPrinted) ? "是" : "否";
            }
        }

        public bool ShowInspectResultLink
        {
            get
            {
                return InspectResult == InspectResults.None && Status != ProjectStatus.Destroyed;
            }
        }

        public bool ShowApplyLink
        {
            get
            {
                return Status == ProjectStatus.InspectResultSuccess;
            }
        }

        public static explicit operator ProjectListModel(Project model)
        {
            return new ProjectListModel()
            {
                Id = model.Id,
                Name = model.Name,
                Address = model.Address,
                EquipmentCertNo = model.Equipment.Eq_CertNo,
                EquipmentNo = model.Equipment.Eq_no,
                InspectNO = model.Equipment.Eq_InspectNO,
                InspectResult = (InspectResults)Enum.Parse(typeof(InspectResults), model.InspectResult),
                HasPrinted = model.HasPrinted,
                Status = (ProjectStatus)Enum.Parse(typeof(ProjectStatus), model.Status),
                InspectTime = model.InspectTime.ToString("yyyy年MM月dd日")
            };
        }


    }

    public class InspectResultEditModel
    {
        public int Id { get; set; }

        [Display(Name = "检测部门")]
        [Required]
        public String InspectDepartment { get; set; }

        [Display(Name = "检测时间")]
        [Required]
        public String InspectTime { get; set; }

        [Display(Name = "检测结果")]
        [Required]
        public String InspectResult { get; set; }

        public static explicit operator InspectResultEditModel(Project model)
        {
            return new InspectResultEditModel()
            {
                Id = model.Id
            };
        }
    }

    public class ProjectApplyModel
    {
        public int Id { get; set; }

        [Display(Name = "司机")]
        [Required]
        public String Driver { get; set; }

        [Display(Name = "上岗证号")]
        [Required]
        public String DriverNO { get; set; }

        [Display(Name = "信号工")]
        public String SignalDriver { get; set; }

        [Display(Name = "上岗证号")]
        public String SignalDriverNO { get; set; }

        [Display(Name = "施工许可证")]
        public String ConstructionPermitNO { get; set; }

        public static explicit operator ProjectApplyModel(Project model)
        {
            return new ProjectApplyModel()
            {
                Id = model.Id
            };
        }
    }

    public class ProjectInspectStartModel
    {
        [Display(Name = "设备备案编号")]
        [Required(ErrorMessage = "设备编号不能为空")]
        public String EquipmentNO { get; set; }
    }

    public class ProjectDetailModel : ProjectAddModel
    {
        [Display(Name = "安装单位")]
        public String DemolitionCompanyPhone { get; set; }

        public InspectResults InspectResult { get; set; }

        public String InspectResultValue
        {
            get
            {
                switch (InspectResult)
                {
                    case InspectResults.Failure:
                        return "未通过";
                    case InspectResults.None:
                        return "还未检验";
                    case InspectResults.Pass:
                        return "通过";
                    default:
                        return "未检验";
                }
            }
        }

        public virtual int PrinterId
        {
            get
            {
                return (HttpContext.Current.Application.Get(SystemVariableEnums.Print.ToString()) as PrintEditModel).Print2Name;
            }
        }

        public DateTime RegisterDate { get; set; }
        public ProjectStatus ProjectStatus { get; set; }

        public ProjectDetailModel(Project model)
            : base(model.Equipment)
        {
            Name = model.Name;
            Address = model.Address;
            EquipmentNO = model.Equipment.Eq_no;
            ConstructionCompany = model.ConstructionCompany;
            ConstructionProjectManager = model.ConstructionProjectManager;
            ConstructionProjectManagerPhoneMobile = model.ConstructionProjectManagerPhoneMobile;
            InspectCompany = model.InspectDepartment;
            InspectTime = (model.InspectTime != new DateTime(1900, 1, 1)) ? model.InspectTime.ToString("yyyy年MM月dd日") : ""; ;
            DemolitionCompany = model.Company.Name;
            DemolitionCompanyOwner = model.Company.Legal;
            DemolitionCompanyPhone = model.Company.PhoneOfLegal;
            ConstructDateTime = (model.ConstructDateTime != new DateTime(1900, 1, 1)) ? model.ConstructDateTime.ToString("yyyy年MM月dd日") : "";
            DemolitionDateTime = (model.DemolitionDateTime != new DateTime(1900, 1, 1)) ? model.DemolitionDateTime.ToString("yyyy年MM月dd日") : "";
            Id = model.Id;
            InspectResult = (InspectResults)Enum.Parse(typeof(InspectResults), model.InspectResult);
            RegisterDate = model.CreateDT;
            Status = (ProjectStatus)Enum.Parse(typeof(ProjectStatus), model.Status);
            Owner = model.EquipmentOwner;
            Mobile = model.EquipmentMobile;

        }
    }

    public class ProjectZhunYunModel : ProjectDetailModel
    {
        public override int PrinterId
        {
            get
            {
                return (HttpContext.Current.Application.Get(SystemVariableEnums.Print.ToString()) as PrintEditModel).Print3Name;
            }
        }

        public String ExpiredDate
        {
            get
            {
                if (Convert.ToDateTime(this.DemolitionDateTime) < Convert.ToDateTime(this.InspectTime).AddYears(1))
                    return Convert.ToDateTime(this.DemolitionDateTime).ToString("yyyy年MM月dd日");
                else
                    return Convert.ToDateTime(this.InspectTime).AddYears(1).ToString("yyyy年MM月dd日");
            }
        }

        public ProjectZhunYunModel(Project p)
            : base(p)
        {
        }

    }

    public class ProjectSearchModel
    {
        [Display(Name = "工程名称")]
        public String Name { get; set; }

        [Display(Name = "设备备案号")]
        public String EquipmentNO { get; set; }

        [Display(Name = "报检号")]
        public String EquipmentInspectNO { get; set; }

        [Display(Name = "是否包含已报拆工程")]
        public String Destory { get; set; }

        [Display(Name = "工程申请开始时间")]
        public String CreatedStartDT { get; set; }

        [Display(Name = "工程申请结束时间")]
        public String CreatedEndDT { get; set; }

        [Display(Name = "检测通过开始时间")]
        public String InspectStartDT { get; set; }

        [Display(Name = "检测通过结束时间")]
        public String InspectEndDT { get; set; }
    }

    public class ProjectTagModel
    {
        [Display(Name = "施工单位")]
        public String ConstructionCompany { get; set; }

        [Display(Name = "工程地址")]
        public String Address { get; set; }

        [Display(Name = "安装单位")]
        public String DemolitionCompany { get; set; }
        public int Id { get; set; }

        public int PrinterId
        {
            get
            {
                return (HttpContext.Current.Application.Get(SystemVariableEnums.Print.ToString()) as PrintEditModel).Print4Name;
            }
        }

        public ProjectTagModel(Project p)
        {
            ConstructionCompany = p.ConstructionCompany;
            Address = p.Address;
            DemolitionCompany = p.Company.Name;
            Id = p.Id;
        }
    }

    public class ProjectBeiAnPrintModel : ProjectDetailModel
    {
        public ProjectBeiAnPrintModel(Project p) : base(p) { }

        public int PrinterId
        {
            get
            {
                return (HttpContext.Current.Application.Get(SystemVariableEnums.Print.ToString()) as PrintEditModel).Print1Name;
            }
        }

        public String AJZName
        {
            get
            {
                return (HttpContext.Current.Application.Get(SystemVariableEnums.AJZInfo.ToString()) as AJZInfoEditModel).Name;
            }
        }
    }

    public class ProjectInspectAgainModel
    {
        public int Id { get; set; }

        public String Name { get; set; }

        [Display(Name = "续检时间")]
        public DateTime InspectTime { get; set; }

        public static explicit operator ProjectInspectAgainModel(Project model)
        {
            return new ProjectInspectAgainModel()
            {
                Id = model.Id,
                Name = model.Name
            };
        }
    }

    public class ProjectEditModel
    {
        public int Id { get; set; }
        public String Owner { get; set; }
        public String Mobile { get; set; }

        public static explicit operator ProjectEditModel(Project model)
        {
            return new ProjectEditModel()
            {
                Id = model.Id,
                Owner = model.EquipmentOwner,
                Mobile = model.EquipmentMobile
            };
        }
    }

    public class ProjectExportModel
    {
        [Display(Name = "项目名称", Order = 1)]
        public String Name { get; set; }
        [Display(Name = "设备备案号", Order = 2)]
        public String EquipmentNo { get; set; }
        [Display(Name = "设备报检号", Order = 3)]
        public String InspectNo { get; set; }
        [Display(Name = "设备合格证号", Order = 4)]
        public String EquipmentCertNo { get; set; }
        [Display(Name = "检测时间", Order = 5)]
        public String InspectTime { get; set; }

        public static explicit operator ProjectExportModel(Project model)
        {
            return new ProjectExportModel()
            {
                Name = model.Name,
                EquipmentCertNo = model.Equipment.Eq_CertNo,
                EquipmentNo = model.Equipment.Eq_no,
                InspectNo = model.Equipment.Eq_InspectNO,
                InspectTime = model.InspectTime.ToString("yyyy年MM月dd日")
            };
        }
    }
}